<template>
  <div>
    <div style="padding: 0 0 10px 0;text-align: left" >
      <!--      <el-select v-model="selectUser.roleId" :value="selectUser.roleId" placeholder="角色" style="width: 150px" @change="getRoleInputs">
              <el-option v-for="item in roleArr" :label="item.roleName" :value="item.id" :key="item.id"></el-option>
            </el-select>-->
      <el-button style="margin: 0 2px" type="primary"><i class="el-icon-search" style="margin: 0 10px 0 0"></i>查询</el-button>
    </div>
    <div style="padding: 0 0 10px 0;text-align: left">
      <el-button style="margin: 0 2px;"  @click="goBack()">上一步</el-button>
      <el-button style="margin: 0 2px;float: right" type="primary" @click="affirm()">确认{{questionNum}}项</el-button>
    </div>
    <div>
      <el-table
          :data="questions"
          style="width: 100%"
          @selection-change="handleSelectionChange"
      >
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column prop="quType" label="题目类型" width="100">
          <template slot-scope="scope">
            {{questionType[scope.row.quType]}}
          </template>
        </el-table-column>
        <el-table-column
            prop="quContent"
            label="题目内容">
        </el-table-column>
        <el-table-column label="难度" width="100">
          <template slot-scope="scope">
            {{questionLevel[scope.row.level]}}
          </template>
        </el-table-column>
        <el-table-column
            prop="bankName"
            width="130"
            label="所属题库">
        </el-table-column>
        <el-table-column
            prop="userTrueName"
            width="100"
            label="创建人">
        </el-table-column>
        <el-table-column
            prop="gmtCreate"
            width="180"
            label="创建时间">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      questionNum:0,
      questions:[],
      questionIds:'',
      oldQuestions:[],
      //复选框数组
      multipleSelection:[],
      questionType:["","单选题","多选题","判断题","简答题"],
      questionLevel:["","简单","中等","困难"]
    }
  },
  methods:{
    goBack(){
      let examId = this.$router.currentRoute.query.examId;

      let updateing = this.$router.currentRoute.query.updateing;

      this.$router.push('/exam/questionList?examId='+examId+"&updateing="+updateing)
    },
    //存问题对象到本地
    affirm(){
      for (let i = 0; i < this.multipleSelection.length; i++) {
        this.oldQuestions.push({
          quContent:this.multipleSelection[i].quContent,
          quType:this.multipleSelection[i].quType,
          questionId:this.multipleSelection[i].id,
          scores:this.multipleSelection[i].level
        });
      }
      console.log(this.oldQuestions)
      let questions = JSON.stringify(this.oldQuestions);
      localStorage.setItem("examQuestions",questions)
      let examId = this.$router.currentRoute.query.examId;
      let updateing = 1;
      this.$router.push('/exam/questionList?examId='+examId+"&updateing="+updateing)
    },
    LoadQuestionArr(){
      let bankId = localStorage.getItem("bankId");
      let redio1 = localStorage.getItem("redio1");
      let examId = this.$router.currentRoute.query.examId;
      if(redio1!='1'&&redio1!='2'){
        this.$message({
          showClose: true,
          message: '请选择组题方式',
          type: 'warning'
        });
        this.$router.push('/exam/questionList?examId='+examId);
        return;
      }
      if(redio1=='1'&&bankId<1){
        this.$message({
          showClose: true,
          message: '您未选择题库',
          type: 'warning'
        });
        this.$router.push('/exam/questionList?examId='+examId)
        return;
      }
      let url = "http://localhost:9282/question/list-majorId?page=1&bankId=";
      if(redio1=='1'){
        url = url+bankId;
      }
      //加载本地缓存数据
      let oldQuestions = localStorage.getItem("examQuestions");
      if(oldQuestions!=null){
        this.oldQuestions = JSON.parse(oldQuestions)
        console.log(this.oldQuestions)
      }else {
        this.oldQuestions = [];
      }
      for (let i = 0; i < this.oldQuestions.length; i++) {
        this.questionIds += this.oldQuestions[i].questionId + ",";
      }
      let jwt = localStorage.getItem("localJwt");
      this.axios
          .create({
            'headers': {
              'Authorization': jwt
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          let questionIds = [];
          this.questions = jsonResult.data.list;
          console.log(this.questions)
          for (let i = 0; i < this.questions.length; i++) {
            if(this.questionIds.indexOf(this.questions[i].id+",")!=-1){
              questionIds.push(i);
            }
          }
          console.log(questionIds);
          for (let i = questionIds.length-1; i >=0 ; i--) {
            this.questions.splice(questionIds[i],1);
          }
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else {
          this.$alert(jsonResult.message, "错误", {
            confirmButtonText: '确定',
            callback: action => {
              console.log(action)
            }
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.questionNum = this.multipleSelection.length;
    }
  },
  mounted() {
    this.LoadQuestionArr()
  }
}
</script>
